import java.io.File;
import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 86136
 * Date: 2025-04-29
 * Time: 18:45
 */
public class Demo7 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入要搜索的目录：");
        String rootDir = scanner.next();
        File rootFile = new File(rootDir);
        if(!rootFile.isDirectory()) {
            System.out.println("输入的路径不合法！");
            return;
        }
        System.out.println("请输入要删除的文件名");
        String delFile = scanner.next();
        scanDir(rootFile,delFile);
    }

    private static void scanDir(File rootFile, String delFile) {
        File[] rootFiles = rootFile.listFiles();
        if(rootFiles == null) {
            return;
        }
        for(File file : rootFiles) {
            if(file.isFile()) {
                delFile(file,delFile);
            }else {
                scanDir(file,delFile);
            }
        }
    }

    private static void delFile(File file, String delFile) {
        Scanner sc = new Scanner(System.in);
        if(file.getName().contains(delFile)) {
            System.out.println(file.getAbsoluteFile());
            System.out.println("是否删除该文件（y/n）");
            String input = sc.next();
            if(input.equalsIgnoreCase("y")) {
                file.deleteOnExit();
            }
        }
    }
}
